package com.css.fxfzaqbz.modules.assess.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.service.AttachmentInfoService;
import com.css.fxfzaqbz.modules.assess.entity.CasAndUrgen;
import com.css.fxfzaqbz.modules.assess.entity.CasualtiesEntity;
import com.css.fxfzaqbz.modules.assess.entity.SecondDisastersEntity;
import com.css.fxfzaqbz.modules.assess.service.CasualtiesService;
import com.css.fxfzaqbz.modules.assess.vo.CasVo;
import com.css.fxfzaqbz.modules.assess.vo.DisasterAssVO;
import com.css.fxfzaqbz.modules.assess2.service.UrgencysettleDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author y1fan
 * @create 2022-09-13 14:43
 * 人口伤亡
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class CasualtiesController {


    @Resource
    private CasualtiesService casualtiesService;
    @Resource
    private AttachmentInfoService attachmentInfoService;
    @Resource
    private UrgencysettleDataService urgencysettleDataService;



    /*
    * 添加人口伤亡成果上传数据
    * */
    @PostMapping("/saveCasualties")
    public RestResponse saveCasualties(@RequestBody CasualtiesEntity casualtiesEntity) {
        RestResponse restResponse = null ;
        try{
           CasualtiesEntity casualtiesEntity1 = casualtiesService.saveCasualties(casualtiesEntity);
            restResponse=RestResponse.succeed(casualtiesEntity1);
        }catch (Exception e){
            log.error("新增失败",e);
            restResponse = RestResponse.fail("新增失败!");
        }
        return restResponse;
    }

    /*
    * 查询所有人口伤亡成果上传数据  过程稿
    * */
    @GetMapping("/getAllCasualties")
    public RestResponse getAllCasualties(String id){
        RestResponse restResponse = null;
        try{
            List<CasualtiesEntity> casualtiesEntities=casualtiesService.getAllCasualties(id);
            restResponse = RestResponse.succeed(casualtiesEntities);
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }
    /*
     * 查询所有人口伤亡成果上传数据  最终成果稿
     * */
    @GetMapping("/getAllCasualtiesTwo")
    public RestResponse getAllCasualtiesTwo(String id){
        RestResponse restResponse = null;
        try{
            List<CasualtiesEntity> casualtiesEntities=casualtiesService.getAllCasualtiesTwo(id);
            restResponse = RestResponse.succeed(casualtiesEntities);
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /*
    * 根据Id 查询一个人口伤亡成果上传数据
    * */
    @GetMapping("/getCasualtiesById")
    public RestResponse<CasualtiesEntity> getCasualtiesById(String id) {
        RestResponse restResponse = null;
        try{
            //对象信息
            CasualtiesEntity casualtiesEntity = casualtiesService.getCasualtiesById(id);
            //附件信息
            AttachmentInfoEntity attach = attachmentInfoService.getAttach(casualtiesEntity.getScFileId());
            Map<String,Object> map=new HashMap<>();
            map.put("casualtiesEntity",casualtiesEntity);
            map.put("attach",attach);
            restResponse = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("获取数据失败!",e);
            restResponse = RestResponse.fail("获取数据失败!");
        }
        return restResponse;
    }

    /*
    * 根据ID删除人口伤亡成果上传数据
    * */
    @PostMapping("/deleteCasualties")
    public RestResponse deleteCasualties(String id){
        try {
            casualtiesService.deleteCasualties(id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }


    /*
    * 修改人口伤亡成果上传数据
    * */
    @PostMapping("/updateCasualties")
    public RestResponse updateCasualties(@RequestBody CasualtiesEntity casualtiesEntity){
        RestResponse restResponse = null;
        try {
            CasualtiesEntity casualtiesEntity1=  casualtiesService.updateCasualtiesById(casualtiesEntity);
            restResponse=RestResponse.succeed(casualtiesEntity1);
        } catch (Exception e) {
            log.error("修改失败",e);
            return RestResponse.fail("修改失败");
        }
        return restResponse;
    }



    /*
     * 过程稿提交到最终成果接口
     * */
    @PostMapping("/firstSubmitCasualties")
    public RestResponse firstSubmitCasualties(String id){
        try {
            casualtiesService.firstSubmitCasualties(id);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }

    /*
     * 是否重复接口
     * */
    @PostMapping("/casualtiesIfRepetition")
    public RestResponse casualtiesIfRepetition(@RequestBody CasualtiesEntity casualtiesEntity){
        try {
            Boolean flag=casualtiesService.casualtiesIfRepetition(casualtiesEntity);
            return RestResponse.succeed(flag);
        } catch (Exception e) {
            log.error("校验失败",e);
            return RestResponse.fail("校验失败");
        }
    }

    /*
     * 添加描述接口
     * */
    @PostMapping("/addCasualtiesDisaster")
    public RestResponse addCasualtiesDisaster(@RequestBody DisasterAssVO disasterAssVO){
        try {
            String describe="";
            if (disasterAssVO.getDisasterAss()!=null && disasterAssVO.getDisasterAss().size()!=0){
                describe = StringUtils.join(disasterAssVO.getDisasterAss(), "@@@");
            }
            casualtiesService.addCasualtiesDisaster(disasterAssVO.getIds(),describe);
            return RestResponse.succeed("添加描述成果");
        } catch (Exception e) {
            log.error("添加描述失败",e);
            return RestResponse.fail("添加描述失败");
        }
    }


    /*
     * 移除接口
     * */
    @PostMapping("/removeCasualtiesDisaster")
    public RestResponse removeCasualtiesDisaster(String id){
        try {
            casualtiesService.removeCasualtiesDisaster(id);
            return RestResponse.succeed("移除成功");
        } catch (Exception e) {
            log.error("移除失败",e);
            return RestResponse.fail("移除失败");
        }
    }

    /*
     * 最终成果提交接口
     * */
    @PostMapping("/secondSubmitCasualties/{ids}")
    public RestResponse secondSubmitCasualties(@PathVariable("ids") List<String> ids){
        try {
            casualtiesService.secondSubmitCasualties(ids);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }


    //----------------------------------------改-------------------------------------------------------
    /*
     * 查询所有人口伤亡及需紧急安置人员成果上传数据  0过程稿  1最终成果
    */
    @GetMapping("/casAndUrgen/getAll")
    public RestResponse getAll(CasVo vo){
        RestResponse restResponse = null;
        try{
            List<CasAndUrgen> casAndUrgens=casualtiesService.getAll(vo);
            restResponse = RestResponse.succeed(casAndUrgens);
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }
    //查看按钮
    @GetMapping("/casAndUrgen/getById")
    public RestResponse getById(CasVo vo){
        RestResponse restResponse = null;
        try{
            if ("需紧急安置人员".equals(vo.getCont())){
                HashMap<String, Object> hashMap = urgencysettleDataService.getUrgencysettleDataById(vo.getId());
                restResponse = RestResponse.succeed(hashMap);
            }else {
                //对象信息
                CasualtiesEntity casualtiesEntity = casualtiesService.getCasualtiesById(vo.getId());
                //附件信息
                AttachmentInfoEntity attach = attachmentInfoService.getAttach(casualtiesEntity.getScFileId());
                Map<String,Object> map=new HashMap<>();
                map.put("casualtiesEntity",casualtiesEntity);
                map.put("attach",attach);
                restResponse = RestResponse.succeed(map);
            }

        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    //提交最终成果
    @PostMapping("/casAndUrgen/submit")
    public RestResponse submit(@RequestBody CasVo vo){
        try {
            casualtiesService.submit(vo);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }
    //下载按钮
    @GetMapping("/casAndUrgen/getPath")
    public RestResponse getPath(CasVo vo){
        try {
            AttachmentInfoEntity entity=casualtiesService.getPath(vo);
            return RestResponse.succeed(entity);
        } catch (Exception e) {
            log.error("获取失败",e);
            return RestResponse.fail("获取失败");
        }
    }
    //删除
    @PostMapping("/casAndUrgen/delete")
    public RestResponse delete(@RequestBody CasVo vo){
        try {
            casualtiesService.delete(vo);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }
    //移除,最终成果退到过程稿
    @PostMapping("/casAndUrgen/remove")
    public RestResponse remove(@RequestBody CasVo vo){
        try {
            casualtiesService.remove(vo);
            return RestResponse.succeed("移除成功");
        } catch (Exception e) {
            log.error("移除失败",e);
            return RestResponse.fail("移除失败");
        }
    }













}
